package org.amse.korovkin.ciphering.view;

import java.awt.event.ActionEvent;
import java.io.*;
import java.util.Scanner;
import javax.swing.*;
import javax.swing.filechooser.FileNameExtensionFilter;
import org.amse.korovkin.ciphering.Main;

public class MainJFrameActions {
    private static final String EXCHANGE_ICON_PATH = "org/amse/korovkin/ciphering/icons/exchange-24.png";
    private static final String CLEAR_ICON_PATH = "org/amse/korovkin/ciphering/icons/clear-24.png";
    private static final String OPEN_ICON_PATH = "org/amse/korovkin/ciphering/icons/open-24.png";
    public static class OpenSrcAction extends AbstractAction {
        CipheringJFrame parent;
        public OpenSrcAction(CipheringJFrame jFrame) {
            parent = jFrame;
            putValue(NAME, "Open");
            putValue(SHORT_DESCRIPTION, "Open Source File");
            ClassLoader cl = Main.class.getClassLoader();
            putValue(LARGE_ICON_KEY, new ImageIcon(cl.getResource(OPEN_ICON_PATH)));
        }
        public void actionPerformed(ActionEvent e) {
            JFileChooser fch;
            FileNameExtensionFilter filter = new FileNameExtensionFilter("TXT files", "txt");
            try {
                fch = new JFileChooser(loadLastFilePath());
                fch.setFileFilter(filter);
            } catch (IOException ex) {
                ex.printStackTrace();
                fch = new JFileChooser(".");
            }
            int returnVal = fch.showOpenDialog(fch);
            if (returnVal == JFileChooser.APPROVE_OPTION) {

                this.parent.setSrcFilePath(fch.getSelectedFile().getPath());
                try {
                    this.parent.setSrcText(fch.getSelectedFile().getPath());
                    saveLastFilePath(fch.getSelectedFile().getPath());
                } catch (IOException ex) {
                    ex.printStackTrace();
                    JOptionPane.showMessageDialog(parent,ex.getMessage()
                        + "Please choose correct SourceFile."
                        , "Error", JOptionPane.ERROR_MESSAGE);
                }
            }
        }
    }
    public static class ExchangeAction extends AbstractAction {
        CipheringJFrame parent;
        public ExchangeAction(CipheringJFrame jFrame) {
            parent = jFrame;
            putValue(NAME, "Exchange");
            putValue(SHORT_DESCRIPTION, "Exchange source and destination Texts");
            putValue(SMALL_ICON, null);
            ClassLoader cl = Main.class.getClassLoader();
            putValue(LARGE_ICON_KEY, new ImageIcon(cl.getResource(EXCHANGE_ICON_PATH)));
        }
        public void actionPerformed(ActionEvent e) {
            parent.exchange();
        }
    }
    public static class ClearSrcAction extends AbstractAction {
        CipheringJFrame parent;
        public ClearSrcAction(CipheringJFrame jFrame) {
            parent = jFrame;
            putValue(NAME, "ClearSRC");
            putValue(SHORT_DESCRIPTION, "Clear Source Text");
            ClassLoader cl = Main.class.getClassLoader();
            putValue(LARGE_ICON_KEY, new ImageIcon(cl.getResource(CLEAR_ICON_PATH)));
        }
        public void actionPerformed(ActionEvent e) {
            parent.clearSrc();
        }
    }
    public static class ClearDstAction extends AbstractAction {
        CipheringJFrame parent;
        public ClearDstAction(CipheringJFrame jFrame) {
            parent = jFrame;
            putValue(NAME, "ClearDST");
            putValue(SHORT_DESCRIPTION, "Clear Destination Text");
            ClassLoader cl = Main.class.getClassLoader();
            putValue(LARGE_ICON_KEY, new ImageIcon(cl.getResource(CLEAR_ICON_PATH)));
        }
        public void actionPerformed(ActionEvent e) {
            parent.clearDst();
        }
    }
    public static void saveLastFilePath(String filePath)
            throws IOException {
        FileWriter fWriter = new FileWriter(CipheringJFrame.LAST_FILE_PATH);
        fWriter.write(filePath);
        fWriter.close();

    }
    public static String loadLastFilePath()
            throws IOException {
        String res;
        try {
            FileInputStream fis = new FileInputStream(CipheringJFrame.LAST_FILE_PATH);
            Scanner scanner = new Scanner(fis);
            res = scanner.nextLine();
            scanner.close();
        } catch (IOException e) {
            throw new IOException("Path for last file isn't correct or lastFilePath.txt doesn't exist");
        }
        return(res);
    }
}
